//package com.example.xhandroidexample.windows.floatwindow;
//
//import android.app.Activity;
//import android.content.pm.ActivityInfo;
//import android.content.res.AssetFileDescriptor;
//import android.media.MediaPlayer;
//import android.util.Log;
//import android.view.SurfaceHolder;
//import android.view.SurfaceView;
//import android.widget.RelativeLayout;
//
//
//public class PlayerHelper {
//
//    private static final String TAG = "PlayerHelper";
//    private MediaPlayer mMediaPlayer;
//    private AssetFileDescriptor fd;
//    private SurfaceHolder mHolder;
//
//    private int videoWidth;
//    private int videoHeight;
//    private SurfaceView mSurfaceView;
//    private Activity mActivity;
//
//    public void init(Activity activity, SurfaceHolder holder, SurfaceView surfaceView) {
//        fd = activity.getResources().openRawResourceFd(R.raw.vid_bigbuckbunny);
//        mMediaPlayer = new MediaPlayer();
//        mActivity = activity;
//        mHolder = holder;
//        mSurfaceView = surfaceView;
////    }
//
//    //准好播放了
//    public void readyPlay() {
////        String url = "http://res.cloudinary.com/liuyuesha/video/upload/v1475978853/广告_bl4dbp.mp4";
////        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//        try {
//            mMediaPlayer.setDataSource(fd);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        mMediaPlayer.setLooping(true);
//        // 把视频画面输出到SurfaceView
//        mMediaPlayer.setDisplay(mHolder);
//        // 通过异步的方式装载媒体资源
//        mMediaPlayer.prepareAsync();
//
//        mMediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
//            @Override
//            public void onBufferingUpdate(MediaPlayer mp, int percent) {
//                Log.d(TAG, "onBufferingUpdate: percent = " + percent);
//            }
//        });
//
//        mMediaPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() {
//            @Override
//            public boolean onInfo(MediaPlayer mp, int what, int extra) {
//                Log.d(TAG, "onInfo: what = " + what + " extra = " + extra);
//                return false;
//            }
//        });
//
//        mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
//            @Override
//            public void onPrepared(MediaPlayer mp) {
//                //装载完毕回调
//                Log.d(TAG, "onPrepared: ");
//                play();
//            }
//        });
//
//        mMediaPlayer.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
//            @Override
//            public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
//                Log.d(TAG, "onVideoSizeChanged: ");
//                changeVideoSize();
//            }
//        });
//    }
//
//    /**
//     * 播放或者暂停
//     */
//    private void play() {
//        Log.d(TAG, "play: mMediaPlayer.isPlaying() = " + mMediaPlayer.isPlaying());
//        if (mMediaPlayer != null) {
//            if (mMediaPlayer.isPlaying()) {
//                mMediaPlayer.pause();
//            } else {
//                mMediaPlayer.start();
//            }
//        }
//    }
//
//    //改变视频的尺寸自适应。
//    public void changeVideoSize() {
//        videoWidth = mMediaPlayer.getVideoWidth();
//        videoHeight = mMediaPlayer.getVideoHeight();
//        Log.d(TAG, "changeVideoSize: videoWidth = " + videoWidth + " videoHeight = " + videoHeight);
//
//        int surfaceWidth = mSurfaceView.getWidth();
//        int surfaceHeight = mSurfaceView.getHeight();
//        Log.d(TAG, "changeVideoSize: surfaceWidth = " + surfaceWidth + " surfaceHeight = " + surfaceHeight);
//
//        //根据视频尺寸去计算->视频可以在sufaceView中放大的最大倍数。
//        float max;
//        if (mActivity.getResources().getConfiguration().orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
//            //竖屏模式下按视频宽度计算放大倍数值
//            max = (float) videoWidth / (float) surfaceWidth;
//        } else {
//            //横屏模式下按视频高度计算放大倍数值
//            max = (float) videoWidth / (float) surfaceHeight;
//        }
//
//        //视频宽高分别/最大倍数值 计算出放大后的视频尺寸
//        videoWidth = (int) Math.ceil((float) videoWidth / max);
//        videoHeight = (int) Math.ceil((float) videoHeight / max);
//
//        Log.d(TAG, "changeVideoSize: change videoWidth = " + videoWidth + " videoHeight = " + videoHeight);
//        //无法直接设置视频尺寸，将计算出的视频尺寸设置到surfaceView 让视频自动填充。
//        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(videoWidth, videoHeight);
//        mSurfaceView.setLayoutParams(params);
//        mSurfaceView.invalidate();
//    }
//
//    protected void onDestroy() {
//        if (mMediaPlayer != null) {
//            if (mMediaPlayer.isPlaying()) {
//                mMediaPlayer.stop();
//            }
//            mMediaPlayer.release();
//            mMediaPlayer = null;
//        }
//    }
//}
